const { expect } = require('chai');
require('./setup.js');

describe('Plugin Integration (No CSS)', () => {
    beforeEach(() => {
        // Clear DOM
        if (document.body) {
            document.body.innerHTML = '';
        }
    });

    it('should create button element when gitbook starts', (done) => {
        // Mock the CSS import by creating a simple plugin without CSS
        const mockPlugin = `
            window.require(['gitbook'], (gitbook) => {
                gitbook.events.bind('start', (e, pluginConfig) => {
                    const config = pluginConfig['gitcode-issue-feedback'];
                    
                    if (!config || !config.repo) {
                        console.warn('GitCode Issue Feedback: Repository configuration is missing');
                        return;
                    }

                    const reportElement = document.createElement('button');
                    reportElement.textContent = config.label || 'Bug Report';
                    reportElement.className = 'gitbook-plugin-gitcode-issue-feedback';
                    reportElement.setAttribute('aria-label', 'Report an issue on GitCode');
                    reportElement.setAttribute('title', 'Click to report an issue about this page');
                    
                    document.body.appendChild(reportElement);
                });
            });
        `;
        
        // Execute the mock plugin
        eval(mockPlugin);
        
        // Simulate gitbook start
        setTimeout(() => {
            const button = document.querySelector('.gitbook-plugin-gitcode-issue-feedback');
            expect(button).to.not.be.null;
            expect(button.textContent).to.equal('Test Report');
            expect(button.getAttribute('aria-label')).to.equal('Report an issue on GitCode');
            done();
        }, 100);
    });

    it('should handle missing configuration gracefully', (done) => {
        // Mock the CSS import by creating a simple plugin without CSS
        const mockPlugin = `
            window.require(['gitbook'], (gitbook) => {
                gitbook.events.bind('start', (e, pluginConfig) => {
                    const config = pluginConfig['gitcode-issue-feedback'];
                    
                    if (!config || !config.repo) {
                        console.warn('GitCode Issue Feedback: Repository configuration is missing');
                        return;
                    }

                    const reportElement = document.createElement('button');
                    reportElement.textContent = config.label || 'Bug Report';
                    reportElement.className = 'gitbook-plugin-gitcode-issue-feedback';
                    reportElement.setAttribute('aria-label', 'Report an issue on GitCode');
                    reportElement.setAttribute('title', 'Click to report an issue about this page');
                    
                    document.body.appendChild(reportElement);
                });
            });
        `;
        
        // Execute the mock plugin
        eval(mockPlugin);
        
        // Simulate gitbook start with no config
        setTimeout(() => {
            const button = document.querySelector('.gitbook-plugin-gitcode-issue-feedback');
            expect(button).to.be.null;
            done();
        }, 100);
    });
});
